package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author ...
	 */
	public class DragableObject extends FlxSprite
	{
		private var wasClicked:Boolean;
		private var lastMousePos:FlxPoint;
		private var mouseCollisionType:String;
		private var boundToScreen:Boolean;
		
		public function DragableObject(X:Number = 0, Y:Number = 0, SimpleGraphic:Class = null, collisionType:String = null, screenBound:Boolean = true ) 
		{
			super(X, Y, SimpleGraphic);
			wasClicked = false;
			mouseCollisionType = collisionType;
			boundToScreen = screenBound;
		}
		
		override public function update():void 
		{
			if (wasClicked && FlxG.mouse.pressed() || isMouseOver() && FlxG.mouse.justPressed())
			{
				if (wasClicked)
				{
					x += FlxG.mouse.x - lastMousePos.x;
					y += FlxG.mouse.y - lastMousePos.y;
				}
				else
				{
					wasClicked = true;
				}
			}
			else
			{
				wasClicked = false;
			}
			
			super.update();
			
			keepOnScreen();
			
			lastMousePos = FlxG.mouse.getWorldPosition();
		}
		
		private function isMouseOver():Boolean
		{
			//add additional mouse collision types as needed
			if ((mouseCollisionType == null || mouseCollisionType == "box") && 
				((FlxG.mouse.x >= x && FlxG.mouse.x <= x + width) && (FlxG.mouse.y >= y && FlxG.mouse.y <= y + height)))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		private function keepOnScreen():void
		{
			if (x < 0)
			{
				x = 0;
			}
			else if (x + width > FlxG.width)
			{
				x = FlxG.width - width
			}
			
			if (y < 0)
			{
				y = 0;
			}
			else if (y + height > FlxG.height)
			{
				y = FlxG.height - height
			}
			
			if (!isMouseOver())
			{
				wasClicked = false;
			}
		}
		
	}

}